package com.example.andylau96.res.Function.Account;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.andylau96.res.Main.MainActivity;
import com.example.andylau96.res.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by AndyLau96 on 2016/12/10.
 */
public class accountFragment extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener
{
    private ListView listview;
    private ImageButton re;
    private LinearLayout title;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.account,container,false);
        listview= (ListView) view.findViewById(R.id.account_list);
        SimpleAdapter adapter=new SimpleAdapter(getActivity(),getData(),R.layout.list_item,new String[]{"text","draw"},new int[]{R.id.list_item_tv,R.id.list_item_im});
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
        title= (LinearLayout) view.findViewById(R.id.account_title);

        re= (ImageButton) view.findViewById(R.id.account_title_return);
        re.setOnClickListener(this);
        return view;
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        map = new HashMap<>();
        map.put("text","修改/查询员工信息");
        map.put("draw",R.drawable.to);
        list.add(map);

        map = new HashMap<>();
        map.put("text","删除员工信息");
        map.put("draw",R.drawable.to);
        list.add(map);

        return list;
    }

    @Override
    public void onClick(View view) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i==0)
        {
            FragmentTransaction ft=getFragmentManager().beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.framelayut,new yChange());
            ft.commit();
        }
        if(i==1)
        {
            FragmentTransaction ft=getFragmentManager().beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.framelayut,new bDelete());
            ft.commit();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }
}
